<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<head>
		<title>计划订单分解下发</title>
		<link rel="shortcut icon" href="../../images/favicon.ico" type="image/x-icon">
		<script language="JavaScript" src="../../js/utils.js" type="text/javascript"></script>
		<script src="../../js/jQuery/jquery-1.8.3.min.js"></script>
	    <script src="../../js/jQuery/jquery.form.js"></script>
		<script type="text/javascript">
			function getQueryString(name) { 
				var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); 
				var r = window.location.search.substr(1).match(reg); 
				if (r != null) return unescape(r[2]); return null; 
			} 
		
			var totalCount  = getQueryString("num");	
			$(document).ready(function(){ 
				$("#planOrderNum").html(totalCount);
				
				 $("#planOrderDecompostNum").keydown(function(event){
					 if( event.keyCode == 8 || (event.keyCode >=48 && event.keyCode <= 57) || (event.keyCode >=37 && event.keyCode <=40) || event.keyCode == 46){
						 return true;
					 }else{
						 return false;
					 }
			　	 });
				
				 //分解预览
				　$("#btnPlanOrderDecompostScan").click(function(){
					var num = $("#planOrderDecompostNum").val();
					if(!/^[0-9]*[1-9][0-9]*$/.test(num)){
						top.isc.showMessage("分解数量必须是正整数",top.isc.OBMessageBar.TYPE_INFO);
						return ;
					}
					$("#planOrderInputTotalNum").html(totalCount);
					var rows = Math.ceil(totalCount/num);
					var tbody = "";
					var rowNum = 0;
					for(var i = 0 ; i < rows; i ++){
						if( (i == rows - 1) && (totalCount % num != 0 )){
							rowNum = totalCount - i * num;
						}else{
							rowNum = num;
						}
						tbody += '<tr><td>'+(i+1)+'</td><td><input type="text" maxlength="6" style="border: 0;width: 100%;height: 100%;text-align: center;"  value="'+rowNum+'"/></td></tr>';
					}
					$("#planOrderDecompostTable tbody").html(tbody);
					
					$("input",$("#planOrderDecompostTable")).keydown(function(event){
						if( event.keyCode == 8 || (event.keyCode >=48 && event.keyCode <= 57) || (event.keyCode >=37 && event.keyCode <=40) || event.keyCode == 46){
							 return true;
						 }else{
							 return false;
						 }
				　	});
					$("input",$("#planOrderDecompostTable")).change(function(){
						var totalValueSum = 0 ;
						$("input",$("#planOrderDecompostTable")).each(function(index){
							if(/^[0-9]*[1-9][0-9]*$/.test($(this).val())){
								totalValueSum += parseInt($(this).val());
							}
						});
						$("#planOrderInputTotalNum").html(totalValueSum);
				　	});
				}); 
				
				//分解下发
				$("#btnPlanOrderDecompost").click(function(){
					//OBMessageBar.TYPE_ERROR
					//TYPE_INFO  TYPE_ERROR  TYPE_SUCCESS  TYPE_WARNING
					var totalValueSum = 0 ;
					var flag = true;
					if( $("input",$("#planOrderDecompostTable")).length == 0 ){
						top.isc.showMessage("请先分解计划订单",top.isc.OBMessageBar.TYPE_INFO);
						return;
					}
					$("input",$("#planOrderDecompostTable")).each(function(index){
						if(/^[0-9]*[1-9][0-9]*$/.test($(this).val())){
							totalValueSum += parseInt($(this).val());
						}else{
							top.isc.showMessage("生产订单数量必须为正整数",top.isc.OBMessageBar.TYPE_INFO);
							flag = false;
							return;
						}
					});
					if(flag){//计算没有异常情况执行 
						if(totalValueSum > totalCount){
							top.isc.showMessage("生产订单数量总和不能超过计划订单数量",top.isc.OBMessageBar.TYPE_INFO);
						}else{
							var data = {};
							data.poId = getQueryString("id");
							data.num = [];
							$("input",$("#planOrderDecompostTable")).each(function(index){
								data.num.push($(this).val());
							});
							
							var serviceUrl = "com.mes.handler.EmesPlanOrderDecompostHandler";
							var callback=function(response, data, request){
								if(data.result){
									top.isc.showMessage("下发生产订单成功",top.isc.OBMessageBar.TYPE_INFO);
								}else{
									top.isc.showMessage(data.msg,top.isc.OBMessageBar.TYPE_ERROR);
								}
							};
							top.OB.RemoteCallManager.call(serviceUrl,data,{},callback);
						}
					}
				});
			});
		</script>
	</head>
<body>
	<div>
		计划订单数量：<span id="planOrderNum"></span>
	</div>
	<br>
	<div>
		<span>下发数量：</span>
		<input type="text" id="planOrderDecompostNum" maxlength="6" value="10"/>
		<input type="button" value="分解计划订单" id="btnPlanOrderDecompostScan">
		<input type="button" value="下发计划订单" id="btnPlanOrderDecompost">
	</div>
	<br>
	
	<table id="planOrderDecompostTable" cellSpacing=0 cellPadding=0 border=1 style="width: 100%;text-align: center;"  >
		<thead>
			<tr>
				<td>生产订单序号</td>
				<td>生产数量(<span id="planOrderInputTotalNum">0</span>)</td>
			</tr>
		</thead>
		<tbody>
		</tbody>
	</table>
	</body>
</html>
